<template>
    <el-select v-model="selectedSupplier" placeholder="请选择供应商" clearable @change="handleSupplierChange">
        <el-option v-for="item in supplierList" :key="item.id" :label="item.name" :value="item.id">
            <span style="float: left">{{ item.name }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">ID: {{ item.id }}</span>
        </el-option>
    </el-select>
</template>

<script>
import { listSupplier } from "@/api/stock/supplier";

export default {
    name: "SupplierSelect",
    props: {
        value: {
            type: [Number, String],
            default: null
        }
    },
    data() {
        return {
            supplierList: [],
            selectedSupplier: this.value
        };
    },
    watch: {
        value(val) {
            this.selectedSupplier = val;
        }
    },
    created() {
        this.getSupplierList();
    },
    methods: {
        getSupplierList() {
            listSupplier().then(response => {
                this.supplierList = response.rows;
            });
        },
        handleSupplierChange(value) {
            this.$emit('input', value);
            this.$emit('change', value);
        }
    }
};
</script>